package com.huawei.deployentity;

import com.huawei.inputentity.Server;

/**
 * 已购买的服务器：服务器编号，剩余空间
 * 该类的id参数是必须的
 */
public class DeployServer extends Server {

    private long id;
    //初始化节点资源，每个节点的CPU和内存在两个节点上均匀分配
    //节点A
    private int nodeARestCPU;
    private int nodeARestMemory;
    //节点B
    private int nodeBRestCPU;
    private int nodeBRestMemory;

    /**
     * @param id 该服务器的编号，必要
     * @param server 当前购买的服务器所属类型
     */
    public DeployServer(long id, Server server) {
        super(server.getServerType(), server.getCPU(),server.getMemory(),server.getHardWareCost(),server.getDailyCost());
        this.id = id;
        nodeARestCPU=super.getCPU()/2;
        nodeARestMemory=super.getMemory()/2;

        nodeBRestCPU=super.getCPU()/2;
        nodeBRestMemory=super.getMemory()/2;
    }
    /**
     * @param server 当前购买的服务器所属类型
     */
    public DeployServer(Server server) {
        super(server.getServerType(), server.getCPU(),server.getMemory(),server.getHardWareCost(),server.getDailyCost());
        nodeARestCPU=super.getCPU()/2;
        nodeARestMemory=super.getMemory()/2;

        nodeBRestCPU=super.getCPU()/2;
        nodeBRestMemory=super.getMemory()/2;
    }

    @Override
    public String toString() {
        return "DeployServer{" +
                "id=" + id +
                ", nodeARestCPU=" + nodeARestCPU +
                ", nodeARestMemory=" + nodeARestMemory +
                ", nodeBRestCPU=" + nodeBRestCPU +
                ", nodeBRestMemory=" + nodeBRestMemory +
                ", serverType=" + getServerType() +
                '}';
    }

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public int getNodeARestCPU() {
        return nodeARestCPU;
    }

    /**
     * 自减
     * @param nodeARestCPU
     */
    public void setNodeARestCPU(int nodeARestCPU) {
        this.nodeARestCPU -= nodeARestCPU;
    }

    public int getNodeARestMemory() {
        return nodeARestMemory;
    }

    /**
     * 自减
     * @param nodeARestMemory
     */
    public void setNodeARestMemory(int nodeARestMemory) {
        this.nodeARestMemory -= nodeARestMemory;
    }

    public int getNodeBRestCPU() {
        return nodeBRestCPU;
    }

    /**
     * 自减
     * @param nodeBRestCPU
     */
    public void setNodeBRestCPU(int nodeBRestCPU) {
        this.nodeBRestCPU -= nodeBRestCPU;
    }

    public int getNodeBRestMemory() {
        return nodeBRestMemory;
    }

    /**
     * 自减
     * @param nodeBRestMemory
     */
    public void setNodeBRestMemory(int nodeBRestMemory) {
        this.nodeBRestMemory -= nodeBRestMemory;
    }

    /**
     * 部署虚拟机<br/>
     * 因为服务器不记录包含的虚拟机，所以只用减少服务器资源即可
     * @param deployVM
     */
    public void deployVM(DeployVM deployVM){
        String node = deployVM.getNode();
        int cpu = deployVM.getCPU();
        int memory = deployVM.getMemory();
        switch (node){
            case "A":
                nodeARestCPU-=cpu;
                nodeARestMemory-=memory;
                break;
            case "B":
                nodeBRestCPU-=cpu;
                nodeBRestMemory-=memory;
                break;
            case "AB":
                nodeARestCPU-=cpu/2;
                nodeARestMemory-=memory/2;
                nodeBRestCPU-=cpu/2;
                nodeBRestMemory-=memory/2;
                break;
        }
    }
    /**
     * 释放虚拟机占用的资源
     * @param deployVM
     */
    public void releaseVM(DeployVM deployVM){
        String node = deployVM.getNode();
        int cpu = deployVM.getCPU();
        int memory = deployVM.getMemory();
        switch (node){
            case "A":
                nodeARestCPU+=cpu;
                nodeARestMemory+=memory;
                break;
            case "B":
                nodeBRestCPU+=cpu;
                nodeBRestMemory+=memory;
                break;
            case "AB":
                nodeARestCPU+=cpu/2;
                nodeARestMemory+=memory/2;
                nodeBRestCPU+=cpu/2;
                nodeBRestMemory+=memory/2;
                break;
        }
    }
}
